package com.lonwin.inspectionmanagement.mvp.presenter;

import android.app.Application;
import android.util.ArrayMap;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.jess.arms.utils.DataHelper;
import com.lonwin.inspectionmanagement.app.AppConstants;
import com.lonwin.inspectionmanagement.app.utils.DataUtil;
import com.lonwin.inspectionmanagement.app.utils.RxUtils;
import com.lonwin.inspectionmanagement.mvp.contract.SortOutRecordSearchContract;
import com.lonwin.inspectionmanagement.mvp.model.api.Api;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.ConfigBean;
import com.lonwin.inspectionmanagement.mvp.model.entity.bean.SortOutBean;

import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 12/18/2018 21:38
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class SortOutRecordSearchPresenter extends BasePresenter<SortOutRecordSearchContract.Model, SortOutRecordSearchContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    ConfigBean configBean;
    @Inject
    public SortOutRecordSearchPresenter(SortOutRecordSearchContract.Model model, SortOutRecordSearchContract.View rootView) {
        super(model, rootView);
        configBean=DataHelper.getDeviceData(mRootView.getMyActivity(),AppConstants.EXTRA_CONFIG_DATA);

    }
    /**
     * 获取已经整理的数据
     */
    public void getDigestedXcjlIndex(String startTime,String endDate,int current,String groupids){
        Map<String,Object> maps=new ArrayMap<>();
        maps.put("beginDate",startTime);
        maps.put("current",current);//页码
        maps.put("endDate",endDate);
        maps.put("gpsid",configBean.getGpsId());
        maps.put("groupids",groupids);//河湖ID(多条以中文逗号分隔)
        maps.put("size",Api.PAGE_SIZE_1);
        if (configBean.getCurrentRole().getRoleCode().equals(AppConstants.ROLE_HZ))
        {
            maps.put("role",configBean.getCurrentRole().getRoleCode());
        }
        mModel.getDigestedXcjlIndex(DataUtil.getJNUrl(configBean)+"/hzxh/xcrecordController/getDigestedXcjl",maps)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<SortOutBean>(mErrorHandler) {
                    @Override
                    public void onNext(SortOutBean sortOutBean) {
                        if (sortOutBean!=null&&sortOutBean.getCode()==0)
                        {
                            mRootView.getDigestedXcjlIndexSuccess(sortOutBean.getData().getRecords());
                            return;
                        }
                        mRootView.getDigestedXcjlIndexSuccess(null);
                        mRootView.showMessage("查询数据异常");
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
